package com.dswz.controller.admin;

import com.dswz.po.User;
import com.dswz.service.SessionService;
import com.dswz.service.UserService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.PageBean;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.dswz.vo.VoUser;
import com.google.gson.Gson;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/admins" })
public class UserAdminController {
	 private Logger logger = LogManager.getLogger(UserAdminController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private SessionService sessionService;

	@RequestMapping(value = { "/getUserList" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object getUserList(String userMobile, String userName, String userCreateTime, Integer page, Integer rows,
			Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("userMobile:" + userMobile + "userName:" + userName + "userCreateTime:" + userCreateTime
					+ "page:" + page + "limit:" + rows + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			PageBean pb = this.userService.getUserList(username, userMobile, userName, userCreateTime,
					Byte.valueOf((byte) 0), page, rows);
			result.setResult(0);
			result.setMessage("OK");
			result.setRows(pb.getContentList());
			result.setTotal(pb.getTotalRecords());
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			result.setResult(2);
			result.setMessage(e.getMessage());
			this.logger.error(e.getMessage(), e);
		}
		return result;
	}

	@RequestMapping(value = { "/editUser" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object editUser(String userId, Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("userId:" + userId + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			VoUser voUser = this.userService.editUser(userId);
			result.setData(voUser);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/saveVerifyUserAuth" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object saveVerifyUserAuth(String userId, Byte userAuthStatus, String userAuthFailReason, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info(
					"userId:" + userId + "userAuthStatus:" + userAuthStatus + "userAuthFailReason:" + userAuthFailReason
							+ "页面来源:" + request.getHeader("referer") + "客户端:" + request.getHeader("user-agent")
							+ " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: " + username);
			if (StringUtils.isEmpty(userId)) {
				this.logger.error("id为空!");
				result.setMessage("id为空!");
				result.setResult(3);
				return result;
			}
			if (userAuthStatus == null) {
				this.logger.error("请选择实名认证状态!");
				result.setMessage("请选择实名认证状态!");
				result.setResult(3);
				return result;
			}
			if ((userAuthStatus.byteValue() == 3) && (StringUtils.isEmpty(userAuthFailReason))) {
				this.logger.error("请填写实名认证失败原因!");
				result.setMessage("请填写实名认证失败原因!");
				result.setResult(3);
				return result;
			}
			User user = new User();
			user.setUserId(userId);
			user.setUserAuthStatus(userAuthStatus);
			user.setUserAuthFailReason(userAuthFailReason);
			user.setUserAuthTime(new Date());
			this.userService.updateUser(user);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}
}
